#include "Protocol.hpp"
#include "Sock.hpp"
#include <unistd.h>
#include <cstring>

void Usage(std::string proc)
{
    std::cout << "Usage:\n\t" << proc << " server_ip server_port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        return 1;
    }

    std::string ip = argv[1];
    uint16_t port = static_cast<uint16_t>(atoi(argv[2]));
    int sock = Sock::Socket();
    Sock::Connect(sock, ip, port);
    std::cout << "connect success!" << std::endl;

    request_t req;
    std::cout << "please input data one#" << std::endl;
    std::cin >> req._x;
    std::cout << "please input data two#" << std::endl;
    std::cin >> req._y;
    std::cout << "please input operator#" << std::endl;
    std::cin >> req._op;
    // 序列化
    std::string json_string = serializationRequest(req);
    write(sock, json_string.c_str(), json_string.size());
    // write(sock, &req, sizeof(req));
    char buffer[1024];
    ssize_t s = read(sock, buffer, sizeof(buffer) - 1);
    if (s > 0)
    {
        buffer[s] = 0;
        response_t resp;
        std::string str = buffer;
        DeserializationResponse(str, resp);
        std::cout << "code[0:success]:" << resp.code << std::endl;
        std::cout << "result:" << resp._result << std::endl;
    }

    // response_t resp;
    // memset(&resp, 0, sizeof(resp));

    // read(sock, &resp, sizeof(resp));

    // std::cout << "code[0:success]:" << resp.code << std::endl;
    // std::cout << "result:" << resp._result << std::endl;
    return 0;
}